<template>
  <div>
    <el-dialog title="添加销售员" :visible.sync="dialogVisible" width="30%">
      <el-form :rules="rules" ref="form" :model="form" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="did">
          <!-- <el-input v-model="form.did" placeholder="请输入手机号"></el-input> -->
          <el-select v-model="form.did" clearable placeholder="选择部门">
            <el-option
              v-for="item in didList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="form.role" clearable placeholder="必选项">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            placeholder="请填写备注"
            type="textarea"
            autosize
          ></el-input>
        </el-form-item>
      </el-form>
      <el-dialog
        width="30%"
        title="请添加账号，密码或备注！"
        :visible.sync="dialogVisible1"
        append-to-body
      >
      </el-dialog>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {create, getRoleList} from "@/api/sales";
  import {list} from "@/api/company";

  export default {
    name: "create",
    data() {
      return {
        dialogVisible: false,
        dialogVisible1: false,
        options: [],
        didList: [],
        form: {
          name: "",
          account: "",
          password: "",
          remark: "",
          role: "",
          phone: "",
          did: "",
        },
        //表单验证
        rules: {
          name: [{required: true, message: "请输入昵称", trigger: "blur"}],
          account: [{required: true, message: "请输入账号", trigger: "blur"}],
          phone: [{required: true, message: "请输入手机号", trigger: "blur"}],
          password: [{required: true, message: "请输入密码", trigger: "blur"}],
          role: [{required: true, message: "请选择角色", trigger: "blur"}],
          did: [{required: true, message: "请选择部门", trigger: "blur"}],
        },
      };
    },
    methods: {
      show() {
        this.form = {
          name: "",
          account: "",
          password: "",
          remark: "",
          role: "",
        };
        getRoleList().then((res) => {
          this.options = res.result;
        });
        this.Didlist();
        this.dialogVisible = true;
      },
      //添加销售员
      add() {
        // this.dialogVisible1 = true;
        this.$refs["form"].validate((valid) => {
          if (valid) {
            let form = {
              name: this.form.name,
              account: this.form.account,
              password: this.form.password,
              remark: this.form.remark,
              role: this.form.role,
              did: this.form.did,
              phone: this.form.phone,
            };
            var phone = /^1[3|4|5|7|8][0-9]\d{8}$/;
            if (!phone.test(this.form.phone)) {
              this.$message.warning("手机号格式错误, 请重新输入");
              return false;
            }
            create(form).then((res) => {
              if (res.status === 200) {
                this.dialogVisible = false;
                this.$message.success("添加成功");
                this.$emit("Ok");
              }
            });
          }
        });
        // if (
        //   this.form.name !== "" &&
        //   this.form.account !== "" &&
        //   this.form.password !== ""
        // ) {
        //   this.dialogVisible1 = false;
        // }
      },
      // 获取部门列表
      Didlist() {
        let form = {
          page: 1,
          size: 40,
        };
        list(form).then((res) => {
          console.log(res);
          this.didList = res.result.records;
        });
      },
    },
  };
</script>

<style scoped>
</style>
